<template>
  <div class="search">
    <van-nav-bar fixed title="商品列表" left-arrow @click-left="$router.go(-1)" />

    <van-search
      readonly
      shape="round"
      background="#ffffff"
      :value="searchName||'搜索商品'"
      show-action
      @click="$router.push('/search')"
    >
      <template #action>
        <van-icon class="tool" name="apps-o" />
      </template>
    </van-search>

    <!-- 排序选项按钮 -->
    <div class="sort-btns">
      <div class="sort-item"  @click="getList('all')" >综合</div>
      <div class="sort-item" @click="getList('sales')" >销量</div>
      <div class="sort-item"  @click="getList('price')" >价格 </div>
    </div>

    <div class="goods-list">
      <GoodsItem v-for="item in searchList" :key="item.goods_id" :item="item"></GoodsItem>
    </div>
  </div>
</template>

<script>
import GoodsItem from '@/components/GoodsItem.vue'
import { getList } from '@/api/product'
export default {
  name: 'SearchIndex',
  components: {
    GoodsItem
  },
  created () {
    this.getList()
  },
  methods: {
    async getList (sortType) {
      const { data: { list } } = await getList({
        categoryId: this.$route.query.categoryId,
        sortType: sortType,
        sortPrice: this.sortPrice,
        goodsName: this.searchName,
        page: this.page
      })
      this.searchList = list.data
      console.log(this.searchList)
    }
  },
  data () {
    return {
      sortPrice: 1, // 0-价格从低到高， 1-价格从高到低
      page: 1, // 商品列表页面
      searchList: [], // 获取的搜索列表
      sortType: 'all'// 按类型搜索
    }
  },
  computed: {
    searchName () {
      return this.$route.query.id
    }
  }
}
</script>

<style lang="less" scoped>
.search {
  padding-top: 46px;
  ::v-deep .van-icon-arrow-left {
    color: #333;
  }
  .tool {
    font-size: 24px;
    height: 40px;
    line-height: 40px;
  }

  .sort-btns {
    display: flex;
    height: 36px;
    line-height: 36px;
    .sort-item {
      text-align: center;
      flex: 1;
      font-size: 16px;
    }
  }
}

.goods-list {
  background-color: #f6f6f6;
}
</style>
